#include "Gp.h"

namespace gp
{
	namespace ParameterBlockHelpers
	{
		void ApplyParameterBlocks(ConstantBufferSetHandle hConstants, gk::GpuProgramHandle hGpuProgram, ParameterBlockContainerHandle hParamBlockContainer)
		{
			const cr::size_t numParamBlocks = hParamBlockContainer->GetNumParameterBlocks();

			for (cr::size_t idxParamBlock = 0; idxParamBlock < numParamBlocks; ++idxParamBlock)
			{
				gp::ParameterBlockHandle hParamBlock = hParamBlockContainer->GetParameterBlock(idxParamBlock);

				const cr::size_t numParams = hParamBlock->GetNumParameters();

				for (cr::size_t idxParam = 0; idxParam < numParams; ++idxParam)
				{
					gp::ParameterHandle hParam = hParamBlock->GetParameter(idxParam);
					gk::GpuProgramConstantHandle hGpuProgramConstant = hGpuProgram->GetConstantByName(hParam->GetName());

					//CR_ASSERT(hGpuProgramConstant.IsValid());
					if (hGpuProgramConstant.IsValid())
					{
						switch (hParam->GetType())
						{
						case gk::GpuProgramParameterType::Float:
							{
								hConstants->SetConstant(hGpuProgramConstant, hParam->GetDatablock().As<float>());
								break;
							}
						case gk::GpuProgramParameterType::Vector:
							{
								hConstants->SetConstant(hGpuProgramConstant, hParam->GetDatablock().As<cr::Vec4f>());
								break;
							}
						case gk::GpuProgramParameterType::Matrix:
							{
								hConstants->SetConstant(hGpuProgramConstant, hParam->GetDatablock().As<cr::Matrix4f>());
								break;
							}
						case gk::GpuProgramParameterType::Int:
							{
								hConstants->SetConstant(hGpuProgramConstant, hParam->GetDatablock().As<int>());
								break;
							}
						case gk::GpuProgramParameterType::Texture:
						case gk::GpuProgramParameterType::Sampler:
							{
								hConstants->SetConstant(hGpuProgramConstant, hParam->GetDatablock().As<gk::TextureHandle>());
								break;
							}
						}
					}
				}
			}
		}

		void FillConstantBuffer(gk::ConstantBufferHandle hConstantBuffer, ParameterBlockContainerHandle hParamBlockContainer)
		{
			
		}
	}
}